using System;
using System.Collections.Generic;

using SBPweb.Modules.Modularity;

namespace SBPweb.Contracts.Implementation
{
	/// <summary>
	/// Class of PersistencyDictionary
	/// </summary>
	public class PersistencyDictionary
	{

		#region Protected members

		IPersistable pSubKey = null;
		Dictionary<string, string> pValues = null;

		#endregion

		#region Constructor logic

		/// <summary>
		/// Contructor of PersistencyDictionary
		/// </summary>
		/// <param name="subKey"></param>
		public PersistencyDictionary(IPersistable subKey)
			: base()
		{
			pSubKey = subKey;
			pValues = new Dictionary<string, string>();
		}

		#endregion

		#region Public members

		/// <summary>
		/// value of the given key
		/// </summary>
		/// <param name="key"></param>
		/// <returns></returns>
		public string this[string key]
		{
			get
			{
				return pValues[key];
			}
			set
			{
				pValues[key] = value;
			}
		}

		/// <summary>
		/// subkey
		/// </summary>
		public string SubKey
		{
			get
			{
				return pSubKey.PersistencyKey;
			}
		}

		/// <summary>
		/// List of keys
		/// </summary>
		public List<string> Keys
		{
			get
			{
				return new List<string>(pValues.Keys);
			}
		}

		#endregion

		#region Methods

		#region Set methods

		/// <summary>
		/// Set string
		/// </summary>
		/// <param name="key">key</param>
		/// <param name="value">value</param>
		public void SetString(string key, string value)
		{
			if (!pValues.ContainsKey(key))
			{
				pValues.Add(key, value);
			}
			else
			{
				pValues[key] = value;
			}
		}

		/// <summary>
		/// Set object
		/// </summary>
		/// <param name="key">key</param>
		/// <param name="value">value</param>
		public void SetObject(string key, object value)
		{
			if (!pValues.ContainsKey(key))
			{
				pValues.Add(key, value.ToString());
			}
			else
			{
				pValues[key] = value.ToString();
			}
		}

		/// <summary>
		/// Set int
		/// </summary>
		/// <param name="key">key</param>
		/// <param name="value">value</param>
		public void SetInt(string key, int value)
		{
			SetObject(key, value);
		}

		/// <summary>
		/// Set double
		/// </summary>
		/// <param name="key">key</param>
		/// <param name="value">value</param>
		public void SetDouble(string key, double value)
		{
			SetObject(key, value);
		}

		/// <summary>
		/// Set bool
		/// </summary>
		/// <param name="key">key</param>
		/// <param name="value">value</param>
		public void SetBoolean(string key, bool value)
		{
			SetObject(key, value);
		}

		#endregion

		#region Get methods

		/// <summary>
		/// Get int
		/// </summary>
		/// <param name="key">key</param>
		/// <returns>value</returns>
		public int GetInt(string key)
		{
			return GetInt(key, null);
		}

		/// <summary>
		/// Get int
		/// </summary>
		/// <param name="key">key</param>
		/// <param name="defaultValue">default value (if not found)</param>
		/// <returns>value</returns>
		public int GetInt(string key, int? defaultValue)
		{
			//if (pValues.ContainsKey(key) || defaultValue == null)
			if (pValues.ContainsKey(key))
			{
				return Int32.Parse(pValues[key]);
			}
			else
			{
				return defaultValue.Value;
			}
		}

		/// <summary>
		/// Get double
		/// </summary>
		/// <param name="key">key</param>
		/// <returns>value</returns>
		public double GetDouble(string key)
		{
			return GetDouble(key, null);
		}

		/// <summary>
		/// Get double
		/// </summary>
		/// <param name="key">key</param>
		/// <param name="defaultValue">default value (if not found)</param>
		/// <returns>value</returns>
		public double GetDouble(string key, double? defaultValue)
		{
			//if (pValues.ContainsKey(key) || defaultValue == null)
			if (pValues.ContainsKey(key))
			{
				return Double.Parse(pValues[key].ToString().Replace('.', ','));
			}
			else
			{
				return defaultValue.Value;
			}
		}

		/// <summary>
		/// Get bool
		/// </summary>
		/// <param name="key">key</param>
		/// <returns>value</returns>
		public bool GetBoolean(string key)
		{
			return Boolean.Parse(pValues[key]);
		}

		/// <summary>
		/// Get bool
		/// </summary>
		/// <param name="key">key</param>
		/// <param name="defaultValue">default value (if not found)</param>
		/// <returns>value</returns>
		public bool GetBoolean(string key, bool? defaultValue)
		{
			//if (pValues.ContainsKey(key) || defaultValue == null)
			if (pValues.ContainsKey(key))
			{
				return Boolean.Parse(pValues[key]);
			}
			else
			{
				return defaultValue.Value;
			}
		}

		/// <summary>
		/// Get string
		/// </summary>
		/// <param name="key">key</param>
		/// <returns>value</returns>
		public string GetString(string key)
		{
			return GetString(key, null);
		}

		/// <summary>
		/// Get string
		/// </summary>
		/// <param name="key">key</param>
		/// <param name="defaultValue">default value (if not found)</param>
		/// <returns>value</returns>
		public string GetString(string key, string defaultValue)
		{
			//if (pValues.ContainsKey(key) || defaultValue == null)
			if (pValues.ContainsKey(key))
			{
				return pValues[key];
			}
			else
			{
				return defaultValue;
			}
		}

		#endregion

		#endregion

	}
}
